package com.tdd.api.server.adapter;


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tdd.api.server.adapter.model.ApprovalCenterList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
public class CrmApiService {


    private final String BASE_URL1 = "https://process-platform-prod.ntzlkjc.com";


    private HashMap<String, String> headMap = new HashMap<>();

    public HashMap<String, String> initHeaderMap(String token) {
        headMap.put("X-Lxy-App", "crm");
        headMap.put("X-Lxy-Platform", "lixiaoyun");
        headMap.put("X-Requested-With", "com.lixiaoyun.aike");
        headMap.put("Access-Token", token);
        log.info("initHeaderMap >> {}", headMap);
        return headMap;

    }

    public List<ApprovalCenterList> getApprovalList(String token) {
        String url = BASE_URL1 + "/api/v1/approval_center/list?scope=todo";
        try {
            String re = HttpUtil.createGet(url)
                    .addHeaders(initHeaderMap(token))
                    .charset("UTF-8")
                    .execute().body();
            log.info("getApprovalList >> re >>> {}", re);
            if (StrUtil.isNotEmpty(re) && JSONUtil.isJson(re)) {
                JSONObject reJson = JSONUtil.parseObj(re);
                if (reJson.getInt("code") == 0 && reJson.get("data") != null) {
                    return JSONUtil.toList(reJson.getJSONObject("data").get("list").toString(), ApprovalCenterList.class);
                }
            }
        } catch (HttpException e) {
            log.error(e.getMessage());
            return null;
        }
        return null;
    }
}
